//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LeastSquareFunction.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/LeastSquareProblem.h>
#include <gen/QL/Math/Optimization/CostFunction.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (Cephei::QL::Math::Optimization::ILeastSquareProblem^ lsp) : CCostFunction(CLeastSquareFunction::typeid)
{
    CLeastSquareProblem^ _Clsp;
    try
    {
#ifdef HANDLE
        _phLeastSquareFunction = NULL;
#endif
        _Clsp = safe_cast<CLeastSquareProblem^> (lsp);
        _Clsp->Lock();
        QuantLib::LeastSquareProblem& _lsp = static_cast<QuantLib::LeastSquareProblem&> (_Clsp->GetReference ()); 
        _ppLeastSquareFunction = new boost::shared_ptr<QuantLib::LeastSquareFunction> (new QuantLib::LeastSquareFunction ( _lsp ));
        SetCostFunction (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Clsp != nullptr) _Clsp->Unlock();
    }
}
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (boost::shared_ptr<QuantLib::LeastSquareFunction>& childNative, Object^ owner) : CCostFunction(CLeastSquareFunction::typeid)
{
#ifdef HANDLE
	_phLeastSquareFunction = NULL;
#endif
	_ppLeastSquareFunction = &childNative;
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
}
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (QuantLib::LeastSquareFunction& childNative, Object^ owner) : CCostFunction(CLeastSquareFunction::typeid)
{
#ifdef HANDLE
	_phLeastSquareFunction = NULL;
#endif
	_ppLeastSquareFunction = new boost::shared_ptr<QuantLib::LeastSquareFunction> (&childNative);
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
    _LeastSquareFunctionOwner = owner;
    _CostFunctionOwner = owner;
}

Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (CLeastSquareFunction^ copy) : CCostFunction(CLeastSquareFunction::typeid)
{
#ifdef HANDLE
	_phLeastSquareFunction = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLeastSquareFunction = new boost::shared_ptr<QuantLib::LeastSquareFunction> (copy->GetShared());
        _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
    }
}
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (System::Type^ t) : CCostFunction(CLeastSquareFunction::typeid)
{
#ifdef HANDLE
	_phLeastSquareFunction = NULL;
#endif
	if (!t->IsSubclassOf(CLeastSquareFunction::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (QuantLib::Handle<QuantLib::LeastSquareFunction>& childNative, Object^ owner)  : CCostFunction(CLeastSquareFunction::typeid)
{
	_phLeastSquareFunction = &childNative;
	_ppLeastSquareFunction = &static_cast<boost::shared_ptr<QuantLib::LeastSquareFunction>>(childNative.currentLink());
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
    _LeastSquareFunctionOwner = owner;
}
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (QuantLib::Handle<QuantLib::LeastSquareFunction> childNative)  : CCostFunction(CLeastSquareFunction::typeid)
{
	_phLeastSquareFunction = &childNative;
	_ppLeastSquareFunction = &static_cast<boost::shared_ptr<QuantLib::LeastSquareFunction>>(childNative.currentLink());
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CLeastSquareFunction::CLeastSquareFunction (QuantLib::LeastSquareFunction childNative)  : CCostFunction(CLeastSquareFunction::typeid)
{
#ifdef HANDLE
	_phLeastSquareFunction = NULL;
#endif
	_ppLeastSquareFunction = new boost::shared_ptr<QuantLib::LeastSquareFunction> (new QuantLib::LeastSquareFunction (childNative));
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppLeastSquareFunction));
}
#endif

Cephei::QL::Math::Optimization::CLeastSquareFunction::~CLeastSquareFunction ()
{
    if (_ppLeastSquareFunction != NULL)
    {
	    delete _ppLeastSquareFunction;
        _ppLeastSquareFunction = NULL;
    }
}
Cephei::QL::Math::Optimization::CLeastSquareFunction::!CLeastSquareFunction ()
{
    if (_ppLeastSquareFunction != NULL)
    {
	    delete _ppLeastSquareFunction;
    }
}
QuantLib::LeastSquareFunction& Cephei::QL::Math::Optimization::CLeastSquareFunction::GetReference ()
{
    if (_ppLeastSquareFunction == NULL) throw gcnew NativeNullException ();
	return **_ppLeastSquareFunction;
}
boost::shared_ptr<QuantLib::LeastSquareFunction>& Cephei::QL::Math::Optimization::CLeastSquareFunction::GetShared ()
{
    if (_ppLeastSquareFunction == NULL) throw gcnew NativeNullException ();
	return *_ppLeastSquareFunction;
}
QuantLib::LeastSquareFunction* Cephei::QL::Math::Optimization::CLeastSquareFunction::GetPointer ()
{
    if (_ppLeastSquareFunction == NULL) throw gcnew NativeNullException ();
	return &**_ppLeastSquareFunction;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LeastSquareFunction>& Cephei::QL::Math::Optimization::CLeastSquareFunction::GetHandle ()
{
	if (_phLeastSquareFunction == NULL)
	{
		_phLeastSquareFunction = new Handle<QuantLib::LeastSquareFunction> (*_ppLeastSquareFunction);
	}
	return *_phLeastSquareFunction;
}
#endif
bool Cephei::QL::Math::Optimization::CLeastSquareFunction::HasNative () 
{
	return (_ppLeastSquareFunction != NULL);
}

Cephei::QL::Math::Optimization::ILeastSquareFunction^ Cephei::QL::Math::Optimization::CLeastSquareFunction::Gradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad_f;
    CArray^ _Cx;
    try
    {
        _Cgrad_f = safe_cast<CArray^> (grad_f);
        _Cgrad_f->Lock();
        QuantLib::Array& _grad_f = static_cast<QuantLib::Array&> (_Cgrad_f->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	(*_ppLeastSquareFunction)->gradient ( _grad_f,  _x );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad_f != nullptr) _Cgrad_f->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CLeastSquareFunction::Value (Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLeastSquareFunction)->value ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CLeastSquareFunction::ValueAndGradient (Cephei::QL::Math::IArray^ grad_f, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cgrad_f;
    CArray^ _Cx;
    try
    {
        _Cgrad_f = safe_cast<CArray^> (grad_f);
        _Cgrad_f->Lock();
        QuantLib::Array& _grad_f = static_cast<QuantLib::Array&> (_Cgrad_f->GetReference ()); 
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLeastSquareFunction)->valueAndGradient ( _grad_f,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrad_f != nullptr) _Cgrad_f->Unlock();
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::ILeastSquareFunction^ Cephei::QL::Math::Optimization::CLeastSquareFunction_Factory::Create (Cephei::QL::Math::Optimization::ILeastSquareProblem^ lsp)
{
    return gcnew CLeastSquareFunction ( lsp);
}
